package com.tgjorgoski.utils;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;
import javax.swing.event.*;

//@author  santhosh kumar - santhosh@in.fiorano.com 
public class DropDownButton extends JButton 
                     implements ChangeListener, PopupMenuListener, ActionListener, PropertyChangeListener{ 
    protected final JButton mainButton = this; 
    protected final JButton arrowButton = new JButton(IconResource.getIconResource("dropdown")); 
 
    private boolean popupVisible = false; 
 
    public DropDownButton(){ 
        mainButton.getModel().addChangeListener(this); 
        arrowButton.getModel().addChangeListener(this); 
        arrowButton.addActionListener(this); 
        arrowButton.setMargin(new Insets(0, 0, 0, 0)); 
        mainButton.addPropertyChangeListener("enabled", this); //NOI18N 
        mainButton.setFocusable(false);
        arrowButton.setFocusable(false);
    } 
 
    /*------------------------------[ PropertyChangeListener ]---------------------------------------------------*/ 
 
    public void propertyChange(PropertyChangeEvent evt){ 
        arrowButton.setEnabled(mainButton.isEnabled()); 
    } 
 
    /*------------------------------[ ChangeListener ]---------------------------------------------------*/ 
 
    public void stateChanged(ChangeEvent e){ 
        if(e.getSource()==mainButton.getModel()){ 
            if(popupVisible && !mainButton.getModel().isRollover()){ 
                mainButton.getModel().setRollover(true); 
                return; 
            } 
            arrowButton.getModel().setRollover(mainButton.getModel().isRollover()); 
            arrowButton.setSelected(mainButton.getModel().isArmed() && mainButton.getModel().isPressed()); 
        }else{ 
            if(popupVisible && !arrowButton.getModel().isSelected()){ 
                arrowButton.getModel().setSelected(true); 
                return; 
            } 
            mainButton.getModel().setRollover(arrowButton.getModel().isRollover()); 
        } 
    } 
 
    /*------------------------------[ ActionListener ]---------------------------------------------------*/ 
 
    public void actionPerformed(ActionEvent ae){ 
         JPopupMenu popup = getPopupMenu(); 
         popup.addPopupMenuListener(this);
         double width = popup.getPreferredSize().getWidth();
         popup.show(mainButton, (int)(mainButton.getWidth()+arrowButton.getWidth()-width), mainButton.getHeight()); 
     } 
 
    /*------------------------------[ PopupMenuListener ]---------------------------------------------------*/ 
 
    public void popupMenuWillBecomeVisible(PopupMenuEvent e){ 
        popupVisible = true; 
        mainButton.getModel().setRollover(true); 
        arrowButton.getModel().setSelected(true); 
    } 
 
    public void popupMenuWillBecomeInvisible(PopupMenuEvent e){ 
        popupVisible = false; 
 
        mainButton.getModel().setRollover(false); 
        arrowButton.getModel().setSelected(false); 
        ((JPopupMenu)e.getSource()).removePopupMenuListener(this); // act as good programmer :)
    } 
 
    public void popupMenuCanceled(PopupMenuEvent e){ 
        popupVisible = false; 
    } 
 
    /*------------------------------[ Other Methods ]---------------------------------------------------*/ 
 
    private JPopupMenu popupMenu = ViewUtils.createAndTrackPopupMenu();
    protected JPopupMenu getPopupMenu() {
    	popupMenu.removeAll();
    	popupMenu.add(new AbstractAction("Test1"){
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
    	popupMenu.add(new AbstractAction("Test2"){
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
    	return popupMenu;
    }
    
    
 
    @Override
	public void setPreferredSize(Dimension preferredSize) {
		super.setPreferredSize(preferredSize);
		Dimension arrowButtonPS = arrowButton.getPreferredSize();
		int arrowH = arrowButtonPS.height;
		Insets bInsets = arrowButton.getBorder().getBorderInsets(arrowButton);
		int toAdd = preferredSize.height-arrowH-bInsets.top-bInsets.bottom;
		int topH = (int)(toAdd/2.0);
		int botH = toAdd-topH+1;
		
        arrowButton.setMargin(new Insets(topH, 0, botH, 0)); 
	}

	public JButton addToToolBar(JToolBar toolbar){
    	JToolBar subTB = new JToolBar();
    	subTB.setFloatable(false);
    	subTB.setBorder(null);
    	subTB.setBorderPainted(false);
    	subTB.setRollover(true);
    	subTB.setMargin(new Insets(0,0,0,0));
    	Dimension prefSize = new Dimension(getPreferredSize().width+arrowButton.getPreferredSize().width, getPreferredSize().height);
    	subTB.setPreferredSize(prefSize);
    	subTB.add(mainButton); 
    	subTB.add(arrowButton);
    	toolbar.add(subTB);
        return mainButton; 
    } 
}